import redis
from Queen.config.queen_config import redis_host, redis_port, db, decode_responses, redis_password
from Queen.wrapper.wrapper import catch_exceptions

redis_connect = None


def redis_connect_factory() -> redis:
    global redis_connect
    if redis_connect is None:
        redis_connect = redis.Redis(host=redis_host, port=redis_port, db=db, decode_responses=decode_responses, password=redis_password)
    return redis_connect


@catch_exceptions
def redis_exit_key(key: str) -> bool:
    global redis_connect
    if redis_connect is None:
        redis_connect = redis_connect_factory()
    try:
        # exists() 返回存在的key数量，>0 表示key存在
        return redis_connect.exists(key) > 0
    except Exception as e:
        # 记录错误并返回False表示key不存在
        print(f"检查Redis key时出错: {e}")
        return False

